package main

/*
logrus是用Go语言实现的一个日志系统，与标准库log完全兼容并且核心API很稳定,是Go语言目前最活跃的日志库
*/
import (
	log "github.com/sirupsen/logrus"
	"os"
)

func init() {
	// 日志格式为JSON而非默认ASCII
	log.SetFormatter(&log.JSONFormatter{})
	// 输出stdout而不是默认stderr，也可为文件
	log.SetOutput(os.Stdout)
	// 只记录警告以上
	log.SetLevel(log.InfoLevel)
}

func main() {
	log.WithFields(log.Fields{
		"animal": "walrus",
		"size":   10,
	}).Info("A group of walrus emerges from the ocean")

	log.WithFields(log.Fields{
		"omg":    true,
		"number": 123,
	}).Warn("The group's number increased tremendously!")

	// 通过日志语句重用字段
	contextLogger := log.WithFields(log.Fields{
		"common": "this is a common field",
		"other":  "I also should be logged always",
	})
	log.WithFields(log.Fields{
		"name": "field",
		"age":  "always",
	}).Info("I'll be logged with common and other field2")
	contextLogger.Info("I'll be logged with common and other field")
	contextLogger.Info("Me too")

	// Fatal之后的日志不会输出
	log.WithFields(log.Fields{
		"omg":    true,
		"number": 500,
	}).Fatal("The ice breaks!")
}
